package com.probox.modules.controller.auth;

import com.probox.common.core.constant.CachePrefix;
import com.probox.common.core.domain.R;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.auth.LoginBody;
import com.probox.common.entity.service.auth.SysLoginService;
import com.probox.common.redis.service.RedisService;
import com.probox.common.security.service.TokenService;
import com.probox.systemApi.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
/**
 * token 控制
 *
 * @author usoft
 */
@RestController
@RequestMapping("/auth/")
public class TokenController
{
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private RedisService redisService;

    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form)
    {
        // 用户登录
        //LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword(),form.getWxCode());
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword(),null);
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    @PostMapping("APPlogin")
    public R<?> APPlogin(@RequestBody LoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.Applogin(form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createCustoemrToken(userInfo));
    }

    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            //删除缓存 重新加载！
            Long userId = loginUser.getUserid();
            redisService.deleteObject(CachePrefix.WEB_USER_INFO + userId);
            redisService.deleteObject(CachePrefix.WEB_USER_ROLE + userId);
            redisService.deleteObject(CachePrefix.WEB_USER_PERMISSION + userId);
            redisService.deleteObject(CachePrefix.WEB_ROUTERS + userId);
            String username = loginUser.getUsername();
            // 删除用户缓存记录
            tokenService.delLoginUser(loginUser.getToken());
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    @PostMapping("refreshToken")
    public R<?> refeshS(HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            // 刷新令牌有效期
            tokenService.reUid(loginUser);
            return R.ok();
        }
        return null;
    }

}
